import axios from "axios";

export default function ajax(url, data = {}, type = "GET") {
  type = type.trim().toLowerCase();

  if (type === "get" || type === "delete") {
    url += "?";
    Object.keys(data).forEach((key) => {
      if (data[key]) {
        url += `key=${data[key]}&`;
      }
    });
    if (url.lastIndexOf("?") == url.length - 1) {
      url = url.slice(0, -1);
    }

    if (url.lastIndexOf("&") == url.length - 1) {
      url = url.slice(0, -1);
    }
    return axios[type](url);
  }
  return axios[type](url, data);
}
